package servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;

import DAO.BaseDAO;
import DTO.Merchaned;
import serviceImpl.MerchanedDAO;

@WebServlet(name = "addmerchaned",urlPatterns = "/addmerchaned")
public class AddMerchanedServlet extends HttpServlet  {

	 private static final long serialVersionUID = 1L;

	 private static final String Base_DIR = "f:\\超市管理系统设计\\merchaned_photo";
	 
	 public AddMerchanedServlet() {
		 
	 }
	 
	 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 try {
	    		String sql = "insert into merchaned(name,photo,money,lb) values(?,?,?,?)";
	    		//设置请求头数据字符集
	            response.setCharacterEncoding("utf-8");
	            response.setContentType("text/html; charset=UTF-8");
	            request.setCharacterEncoding("utf-8");
	            SmartUpload su = new SmartUpload();//创建上传组件对象
	            su.initialize(this, request, response);
	            su.setAllowedFilesList("jpg,jpeg,png,gif");//设置允许上传的文件类型
	            su.setMaxFileSize(1024*1024*2);//设置单个文件的最大大小
	            su.upload();//开始上传
	            Files files = su.getFiles();//获取上传文件对象，可以看成是一个数组，但是并不是

	            File file = files.getFile(0);//获取第一个文件对象
	            String fname =null;
	            try {
	                if(file.getSize()>0){//判断文件大小
	                    String ext = file.getFileExt();//获取文件后缀
	                    fname = UUID.randomUUID() + "." + ext;//对文件进行重命名
	                    System.out.println(Base_DIR + java.io.File.separator + fname);
	                    file.saveAs(Base_DIR + java.io.File.separator + fname);//按照指定的路径对文件进行存储
	                }
	            }catch(Exception e){
	                e.printStackTrace();
	            }
	            
	          //获取页面上传来的表单数据
	            String name = su.getRequest().getParameter("name").trim();
	            String money = su.getRequest().getParameter("money").trim();
	            String lb = su.getRequest().getParameter("lb").trim();
	            
	            BaseDAO<Merchaned> md = new MerchanedDAO();
	            Merchaned merchaned = new Merchaned();
	            merchaned.setName(name);
	            merchaned.setMoney(money);
	            merchaned.setLb(lb);
	            merchaned.setPhoto(fname);
	            
	            try {
	    			if(md.add(merchaned, sql)) {
	                    request.setAttribute("msg","新增成功");
	                    request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);
	    			}else {
	                    request.setAttribute("error", "新增失败");
	                    request.getRequestDispatcher("merchaned_list.jsp").forward(request, response);
	    			}
	    		} catch (Exception e) {
	    			e.printStackTrace();
	    			System.out.println(e.getMessage());
	    		}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
			
	 }
	 
	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 doPost(request, response);
     }

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	 service(request, response);
     }
}
